from PIL import Image

#python -u "d:\pixiv\YgoCardPicture.py"

def fill(origin,TemplateX,TemplateY,path,picture):
    if TemplateX/TemplateY==origin.width/origin.height:
        print("pattern match")
    elif TemplateX/TemplateY < origin.width/origin.height:#長不夠
        CropSize = int((origin.width*TemplateY/TemplateX-origin.height)/2)
        CropPart1 = origin.crop((0,0,origin.width,CropSize))
        CropPart2 = origin.crop((0,origin.height-CropSize,origin.width,origin.height))
        NewImage = Image.new('RGB', (origin.width,origin.height+CropSize*2))
        NewImage.paste(CropPart1,(0,0))
        NewImage.paste(origin,(0,CropSize))
        NewImage.paste(CropPart2,(0,CropSize+origin.height))
    elif TemplateX/TemplateY>origin.width/origin.height:#寬不夠
        CropSize = int((origin.height*TemplateX/TemplateY-origin.width)/2)
        CropPart1 = origin.crop((0,0,CropSize,origin.height))
        CropPart2 = origin.crop((origin.width-CropSize,0,origin.width,origin.height))
        NewImage = Image.new('RGB', (origin.width+CropSize*2,origin.height))
        NewImage.paste(CropPart1,(0,0))
        NewImage.paste(origin,(CropSize,0))
        NewImage.paste(CropPart2,(CropSize+origin.width,0))
    NewImage.show()
    print("save(1) or not(2)")
    SaveOption = int(input())
    if SaveOption == 1:
        NewImage.save(path+picture[:len(picture)-4]+"2"+picture[len(picture)-4:])
        print("已保存到"+path+picture[:len(picture)-4]+"2"+picture[len(picture)-4:]+",尺寸為",NewImage.width,"x",NewImage.height)

def cut(origin,TemplateX,TemplateY,path,picture):
    if TemplateX/TemplateY==origin.width/origin.height:
        print("pattern match")
    elif TemplateX/TemplateY<origin.width/origin.height:#過寬
        CropSize = int((origin.width-TemplateX*origin.height/TemplateY)/2)
        NewImage = Image.new('RGB', (origin.width,origin.height+CropSize*2))
        print("左對齊1,居中2,右對齊3")
        option2 = int(input())
        if option2 == 1:
            NewImage = origin.crop((0,0,origin.width-2*CropSize,origin.height))
        elif option2 == 2:
            NewImage = origin.crop((CropSize,0,origin.width-CropSize,origin.height))
        elif option2 == 3:
            NewImage = origin.crop((2*CropSize,0,origin.width,origin.height))   
    else:#過長
        CropSize = int((origin.height-origin.width*TemplateY/TemplateX)/2)
        print("上對齊1,居中2,下對齊3")
        option2 = int(input())
        if option2 == 1:
            NewImage = origin.crop((0,0,origin.width,origin.height-2*CropSize))
        elif option2 == 2:
            NewImage = origin.crop((0,CropSize,origin.width,origin.height-CropSize))
        else:
            NewImage = origin.crop((0,2*CropSize,origin.width,origin.height))
    NewImage.show()
    print("save(1) or not(2)")
    SaveOption = int(input())
    if SaveOption == 1:
        NewImage.save(path+picture[:len(picture)-4]+"2"+picture[len(picture)-4:])
        print("已保存到"+path+picture[:len(picture)-4]+"2"+picture[len(picture)-4:]+",尺寸為",NewImage.width,"x",NewImage.height)

path = "D:\\image\\草莓恐慌\\"
picture = "42477208_p0.png"
origin = Image.open(path+picture)
print("尺寸為",origin.width,"x",origin.height,"請選擇模式,填充1,裁剪2")
option1 = int(input())
if option1 == 1:#177,254
    fill(origin,177,254,path,picture)
else:
    cut(origin,177,254,path,picture)